package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.api.IAuthClient;
import com.heima.api.LikesBehaviorClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApCollection;

import com.heima.model.article.vo.StateResult;
import com.heima.model.behavior.dto.BehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author JinCc
 * @since 2024-08-22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {

    private final ApCollectionMapper collectionMapper;
    private final ApArticleMapper apArticleMapper;
    private final LikesBehaviorClient likesBehaviorClient;
    private final IAuthClient followBehaviorClient;

    /**
     * 文章收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collection(BehaviorDto dto) {
        if (ObjectUtils.isEmpty(dto)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.HALF_SUCCESS);
        }
        WmUser user = WmThreadLocalUtil.getUser();
        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(dto.getEntryId()), ApCollection::getArticleId, dto.getEntryId());
        wrapper.eq(!ObjectUtils.isEmpty(user.getId()), ApCollection::getEntryId, user.getId());
        ApCollection apCollection1 = collectionMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(apCollection1)){
            ApArticle apArticle = apArticleMapper.selectById(dto.getEntryId());
            ApCollection apCollection = new ApCollection();
            apCollection.setEntryId(user.getId());
            apCollection.setType(Short.valueOf(dto.getType()));
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setCollectionTime(LocalDateTime.now());
            apCollection.setOperation(Short.valueOf(dto.getOperation()));
            Instant instant = apArticle.getPublishTime().toInstant();
            LocalDateTime dateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
            apCollection.setPublishedTime(dateTime);
            collectionMapper.insert(apCollection);
        } else {
            apCollection1.setOperation(Short.valueOf(dto.getOperation()));
            collectionMapper.updateById(apCollection1);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 文章行为数据回显
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(BehaviorDto dto) {
        WmUser user = WmThreadLocalUtil.getUser();
        dto.setUserId(user.getId());
        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(dto.getArticleId()), ApCollection::getArticleId, dto.getArticleId());
        wrapper.eq(!ObjectUtils.isEmpty(user.getId()), ApCollection::getEntryId, user.getId());
        ApCollection apCollection = collectionMapper.selectOne(wrapper);
        StateResult stateResult = new StateResult();
        //远程调用获取喜欢状态
        String likeArticle = likesBehaviorClient.likeArticle(dto);
        stateResult.setIslike(getResult(likeArticle));
        //设置收藏状态
        /*String ac =  (String.valueOf(apCollection.getOperation())==null)?"1":String.valueOf(apCollection.getOperation());*/
        if (ObjectUtils.isEmpty(apCollection)){
            stateResult.setIscollection(getResult("1"));
        }else {
            stateResult.setIscollection(getResult(String.valueOf(apCollection.getOperation())));
        }

        //设置不喜欢状态
        String unLikeArticle = likesBehaviorClient.unLikeArticle(dto);
        stateResult.setIsunlike(getResult(unLikeArticle));

        //设置关注状态
        String followArticle = followBehaviorClient.followArticle(dto);
        stateResult.setIsfollow(getResult(followArticle));
        return ResponseResult.okResult(stateResult);
    }

    private Boolean getResult(String state) {
        if ("1".equals(String.valueOf(state)) ){
            return false;
        }else {
            return true;
        }
    }
}
